package com.lvan.service.order;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lvan.bean.BaseException;
import com.lvan.bean.CheckStock;
import com.lvan.bean.Page;
import com.lvan.constant.ValidateRecord;
import com.lvan.dao.mapper.CheckStockMapper;
import com.lvan.service.AbstractService;
import com.lvan.service.interfaze.order.ICheckStockService;
import com.lvan.util.InputValidate;

@Service("checkStockService")
public class CheckStockService extends AbstractService implements ICheckStockService {
	@Autowired
	private CheckStockMapper checkStockMapper;
	private InputValidate<CheckStock> validator = new InputValidate<CheckStock>();
	@Override
	public void add(CheckStock o) throws BaseException {
		Set<ValidateRecord> s = validator.validate(o, true);
		if(!s.isEmpty()) {
			StringBuilder sb = new StringBuilder();
			for (ValidateRecord constraintViolation : s) {
				sb.append(constraintViolation.getMessage() + "; ");
			}
			throw new BaseException("", REQUEST_PARAM_ERROR, sb.toString());
		}
		this.checkStockMapper.add(o);
	}

	@Override
	public void delete(Serializable ids) throws BaseException {
		if(ids!=null) {
			this.checkStockMapper.delete(ids);
		}
	}

	@Override
	public void update(CheckStock o) throws BaseException {
		Set<ValidateRecord> s = validator.validate(o, false);
		if(!s.isEmpty()) {
			StringBuilder sb = new StringBuilder();
			for (ValidateRecord constraintViolation : s) {
				sb.append(constraintViolation.getMessage() + "; ");
			}
			throw new BaseException("", REQUEST_PARAM_ERROR, sb.toString());
		}
		this.checkStockMapper.update(o);
	}

	@Override
	public CheckStock getByID(Serializable id) {
		return this.checkStockMapper.getByID(id);
	}

	@Override
	public int count(CheckStock o) {
		return this.checkStockMapper.count(o);
	}

	@Override
	public List<CheckStock> query(CheckStock o, Page page) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("checkStock", o);
		params.put("page", page.getPageSql());
		params.put("orderBy", o.convertField2Column());
		page.setTotal(this.count(o));
		return this.checkStockMapper.query(params);
	}

}
